import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {SFSchema} from '@delon/form';
import {STColumn} from '@delon/abc';
import {NzModalService} from 'ng-zorro-antd';
import {BaseGridComponent, BaseService, Page} from '@sb/base';
import {OuOrgService} from '../../service/ou-org.service';
import {OuOrgSearchForm} from '../../entity/ou-org-search-form';
import {OuOrg} from '../../entity/ou-org';

@Component({
    selector: 'ou-dept-tab',
    templateUrl: './ou-dept-tab.component.html',
    styleUrls: ['./ou-dept-tab.component.less'],
    providers: [{
        provide: BaseService,
        useClass: OuOrgService
    }]
})
export class OuDeptTabComponent implements OnInit {

    typeIcon = {
        root: 'dr:hy-root',
        ORG: 'dr:hy-module',
        DEPT: 'dr:hy-terminal',
        USER: 'dr:user_yw'
    };

    @Output() dataChange = new EventEmitter<any>();

    @Output() itemClickEvent = new EventEmitter<any>();

    @ViewChild('baseGrid', {static: true})
    baseGrid: BaseGridComponent;
    schema: SFSchema = {
        properties: {
            orgName: {
                type: 'string',
                title: '部门名称',
            }
        },
        required: ['orgName']
    };
    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'roleId',
            width: '10%',
            className: 'text-center word-wrap',
        }, {
            title: '部门名称',
            index: 'orgName',
            width: '15%',
            className: 'text-center word-wrap',
        }
    ];

    constructor(private modal: NzModalService) {
    }

    @Input()
    set parentOrgId(_parentOrgId: string) {
        if (_parentOrgId) {
            this.baseGrid.baseParam = {parentOrgId: _parentOrgId, orgType: 'DEPT'};
            this.baseGrid.searchForm = {parentOrgId: _parentOrgId, orgType: 'DEPT'} as Page;
            if (this.baseGrid.tableDataObservable) {
                this.baseGrid.reload();
            }
        }
    }

    ngOnInit() {
    }

    onDataReload() {
        this.dataChange.emit();
    }

    itemClick(org: OuOrg) {
        this.itemClickEvent.emit(org.orgId);
    }

}
